1.3. Running the Project 运行项目

项目有了,进入项目的跟目录(即 \simple-service )现在先测试运行下:

  1. $ mvn clean test

项目将会被编译,并且进行单元测试

  1. -------------------------------------------------------
  2. T E S T S
  3. -------------------------------------------------------
  4. Running com.example.MyResourceTest
  5. 八月 30, 2014 9:35:06 上午 org.glassfish.grizzly.http.server.NetworkListener sta
  6. rt
  7. INFO: Started listener bound to [localhost:8080]
  8. 八月 30, 2014 9:35:06 上午 org.glassfish.grizzly.http.server.HttpServer start
  9. INFO: [HttpServer] Started.
  10. 八月 30, 2014 9:35:07 上午 org.glassfish.grizzly.http.server.NetworkListener shu
  11. tdownNow
  12. INFO: Stopped listener bound to [localhost:8080]
  13. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.485 sec
  14. Results :
  15. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  16. [INFO] ------------------------------------------------------------------------
  17. [INFO] BUILD SUCCESS
  18. [INFO] ------------------------------------------------------------------------
  19. [INFO] Total time: 02:21 min
  20. [INFO] Finished at: 2014-08-30T09:35:07+08:00
  21. [INFO] Final Memory: 13M/31M
  22. [INFO] ------------------------------------------------------------------------

上面可以看看到测试通过,下面我们用标准模式运行项目:

  1. $ mvn exec:java

运行结果如下:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethrea
  4. ded.SingleThreadedBuilder with a thread count of 1
  5. [INFO]
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] Building simple-service 1.0-SNAPSHOT
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO]
  10. [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ simple-service >>>
  11. [INFO]
  12. [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ simple-service <<<
  13. [INFO]
  14. [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ simple-service ---
  15. 八月 30, 2014 9:36:57 上午 org.glassfish.grizzly.http.server.NetworkListener sta
  16. rt
  17. INFO: Started listener bound to [localhost:8080]
  18. 八月 30, 2014 9:36:57 上午 org.glassfish.grizzly.http.server.HttpServer start
  19. INFO: [HttpServer] Started.
  20. Jersey app started with WADL available at http://localhost:8080/myapp/applicatio
  21. n.wadl
  22. Hit enter to stop it...

项目已经运行,项目的 WADL 描述存在于http://localhost:8080/myapp/application.wadl URI中,将该URI在控制台 以curl命令执行或者浏览器中运行,就能看到该 WADL 描述以 XML 格式展示。
WADL

更多 WADL的内容,请查考

Chapter 16, WADL Support
浏览器

接下来试下与部署在 /myresource 下面的资源的交互。将资源的URL输入浏览器,或者在控制台用curl命令执行(译者注:如果没有安装curl,请参考curl安装):

  1. $ curl http://localhost:8080/myapp/myresource
  2. Got it!

curl

-i命令获取所有回应的头文件信息:

  1. $ curl -i http://localhost:8080/myapp/myresource
  2. HTTP/1.1 200 OK
  3. Content-Type: text/plain
  4. Date: Sat, 30 Aug 2014 02:23:25 GMT
  5. Content-Length: 7
  6. Got it!

注意到Content-Type: text/plain是在 MyResource 类中用@Produces 注解的。

如果想看到更多返回信息,可以变换不同的 curl 命令参数。举例:

  1. $ curl -v http://localhost:8080/myapp/myresource
  2. * Adding handle: conn: 0x5bc180
  3. * Adding handle: send: 0
  4. * Adding handle: recv: 0
  5. * Curl_addHandleToPipeline: length: 1
  6. * - Conn 0 (0x5bc180) send_pipe: 1, recv_pipe: 0
  7. * About to connect() to localhost port 8080 (#0)
  8. * Trying 127.0.0.1...
  9. * Connected to localhost (127.0.0.1) port 8080 (#0)
  10. > GET /myapp/myresource HTTP/1.1
  11. > User-Agent: curl/7.33.0
  12. > Host: localhost:8080
  13. > Accept: */*
  14. >
  15. < HTTP/1.1 200 OK
  16. < Content-Type: text/plain
  17. < Date: Sat, 30 Aug 2014 01:55:06 GMT
  18. < Content-Length: 7
  19. <
  20. Got it!* Connection #0 to host localhost left intact

链接